package com.gklyj.controller.administer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.gklyj.service.feedbackService;
import com.gklyj.service.positionService;
import com.gklyj.service.userService;
import com.gklyj.service.zixunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;

@Controller
@Transactional
public class IndexController {

    @Autowired
    userService userService;

    @Autowired
    positionService positionService;

    @Autowired
    zixunService zixunService;

    @Autowired
    feedbackService feedbackService;

    //跳转首页
    @GetMapping("/toWelcomePage")
    public String toWelcomePage(Model model){
        //获取当前年月日
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示时分秒
        Timestamp now= new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String today= df.format(now);
        //将获取的当天时间减去一天
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天
        String yesterday = df.format(calendar.getTime());
        //获取当天 和 昨天 发布资讯的数量
        Integer todayZixun = zixunService.GetOneDayZixunNumber(today);
        Integer yesterdayZixun = zixunService.GetOneDayZixunNumber(yesterday);

        //获取当天 和 昨天 发布岗位的数量
        int todayGangwei = positionService.GetOneDayGangweiNumber(today);

        int yesterdayGangwei = positionService.GetOneDayGangweiNumber(yesterday);

        //获取资讯 和 昨天 发布资讯的数量差值
        int zixunTrend = todayZixun-yesterdayZixun;
        //获取岗位 和 昨天 发布岗位的数量差值
        int gangweiTrend = todayGangwei-yesterdayGangwei;

        model.addAttribute("todayZixun",todayZixun);
        model.addAttribute("zixunTrend",zixunTrend);
        model.addAttribute("todayGangwei",todayGangwei);
        model.addAttribute("gangweiTrend",gangweiTrend);
        return "administer/welcome";
    }

    //未查看反馈信息数量需要单独查出，需要实时刷新
    @GetMapping("/getNewFeedback")
    @ResponseBody
    public Integer getNewFeedback(){
        return feedbackService.SearchNewFeedback();
    }


    //数据统计页面
    @GetMapping("/toDateStatisticsPage")
    public String toDateStatisticsPage(Model model){
        model.addAttribute("qiuzhi",userService.GetQiuzhiNumber());
        model.addAttribute("zhaoping",userService.GetZhaopingNumber());
        model.addAttribute("gangwei",positionService.GetGangweiNumber());
        model.addAttribute("zixun",zixunService.GetZixunNumber());
        return "administer/data-statistics";
    }

    /*
    用于统计图，查找各年级学生
     */
    @GetMapping("/getQzNumber")
    @ResponseBody
    public JSONObject getQzNumber(){
        JSONObject qzgrade = new JSONObject();
        qzgrade.put("grade2019",userService.getQzNumberByGrades("2019级"));
        qzgrade.put("grade2020",userService.getQzNumberByGrades("2020级"));
        qzgrade.put("grade2021",userService.getQzNumberByGrades("2021级"));
        qzgrade.put("grade2022",userService.getQzNumberByGrades("2022级"));
        return qzgrade;
    }


    /*
    用于折线图数据，查找各月份岗位数量和资讯数量
     */
    @GetMapping("/getGwAndZxNumber")
    @ResponseBody
    public JSONArray getGwAndZxNumber(){

        //将各月份发布的岗位数量保存为json格式
        JSONObject gwnumber = new JSONObject();
        gwnumber.put("3月",positionService.GetGwNumberByTime("03"));
        gwnumber.put("4月",positionService.GetGwNumberByTime("04"));
        gwnumber.put("5月",positionService.GetGwNumberByTime("05"));
        gwnumber.put("6月",positionService.GetGwNumberByTime("06"));
        gwnumber.put("7月",positionService.GetGwNumberByTime("07"));

        //将各月份发布的资讯数量保存为json格式
        JSONObject zxnumber = new JSONObject();
        zxnumber.put("3月",zixunService.GetZixunNumberByTime("03"));
        zxnumber.put("4月",zixunService.GetZixunNumberByTime("04"));
        zxnumber.put("5月",zixunService.GetZixunNumberByTime("05"));
        zxnumber.put("6月",zixunService.GetZixunNumberByTime("06"));
        zxnumber.put("7月",zixunService.GetZixunNumberByTime("07"));

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(gwnumber);
        jsonArray.add(zxnumber);

        System.out.println(jsonArray);
        return jsonArray;
    }

}
